package video

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/video"
)

// 定义bbs的service提供xkbbs的数据curd的操作

type XkVideoCategoryService struct{}

// @author: feige
// @function: CreateXkVideoCategory
// @description: 创建文章
// @param: e video.XkVideoCategory
// @return: err error
func (xkcategory *XkVideoCategoryService) CreateXkVideoCategory(xkVideoCategory *video.XkVideoCategory) (err error) {
	// 1： 获取数据的连接对象 如果执行成功err是nil，如果失败就把失败告诉
	err = global.GVA_DB.Create(xkVideoCategory).Error
	return err
}

//@author: feige
//@function: UpdateXkVideoCategory
//@description: 更新文章
//@param: e *model.ExaCustomer
//@return: err error

func (xkcategory *XkVideoCategoryService) UpdateXkVideoCategory(xkVideoCategory *video.XkVideoCategory) (err error) {
	//err = global.GVA_DB.Save(xkVideoCategory).Error
	//return err
	err = global.GVA_DB.Model(xkVideoCategory).Updates(xkVideoCategory).Error
	return err
}

// @author: feige
// @function: DeleteXkVideoCategory
// @description: 删除帖子
// @param: e model.DeleteXkVideoCategory
// @return: err error
func (xkcategory *XkVideoCategoryService) DeleteXkVideoCategory(xkVideoCategory *video.XkVideoCategory) (err error) {
	err = global.GVA_DB.Delete(&xkVideoCategory).Error
	return err
}

// @author: feige
// @function: DeleteXkVideoCategoryById
// @description: 根据ID删除帖子
// @param: e model.DeleteXkVideoCategoryById
// @return: err error
func (xkcategory *XkVideoCategoryService) DeleteXkVideoCategoryById(id uint) (err error) {
	var xkVideoCategory video.XkVideoCategory
	err = global.GVA_DB.Where("id = ?", id).Delete(&xkVideoCategory).Error
	return err
}

// @author: feige
// @function: GetXkVideoCategory
// @description: 根据ID获取帖子信息
// @param: id uint
// @return: xkVideoCategory *video.XkVideoCategory, err error
func (xkcategory *XkVideoCategoryService) GetXkVideoCategory(id uint) (xkVideoCategory *video.XkVideoCategory, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&xkVideoCategory).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomerInfoList
//@description: 分页获取客户列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (xkcategory *XkVideoCategoryService) LoadXkVideoCategoryPage(info request.PageInfo) (list interface{}, total int64, err error) {
	// 获取分页的参数信息
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	// 准备查询那个数据库表
	db := global.GVA_DB.Model(&video.XkVideoCategory{})

	// 准备切片帖子数组
	var XkVideoCategoryList []video.XkVideoCategory

	// 加条件
	if info.Keyword != "" {
		db = db.Where("title like ?", "%"+info.Keyword+"%")
	}

	// 排序默时间降序降序
	db = db.Order("created_at desc")

	// 查询中枢
	err = db.Count(&total).Error
	if err != nil {
		return XkVideoCategoryList, total, err
	} else {
		// 执行查询
		err = db.Limit(limit).Offset(offset).Find(&XkVideoCategoryList).Error
	}

	// 结果返回
	return XkVideoCategoryList, total, err
}
